//
//  TuijianView.m
//  壳牌喜力促销积分商城
//
//  Created by TUSM on 15/3/16.
//  Copyright (c) 2015年 xrd. All rights reserved.
//

#import "TuijianView.h"
#import "TuijianModel.h"
@interface TuijianView ()
@property (nonatomic, weak)UIImageView *icon;
@property (nonatomic, weak)UILabel *title;
@property (nonatomic, weak)UILabel *buyCount;

@end
@implementation TuijianView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 图片
        UIImageView *icon = [[UIImageView alloc] init];
        icon.contentMode =  UIViewContentModeScaleToFill;
        [self addSubview:icon];
        self.icon = icon;
        // 标题
        UILabel *title = [[UILabel alloc] init];
        [self addSubview:title];
        self.title = title;
        // 积分数
        UILabel *buyCount = [[UILabel alloc] init];
        buyCount.font = [UIFont systemFontOfSize:15];
        buyCount.numberOfLines = 0;
        [self addSubview:buyCount];
        self.buyCount = buyCount;
    }
    return self;
}
// 通过传过来的模型赋值数据
- (void)setModel:(TuijianModel *)model
{
    _model = model;
    
    self.icon.image = [UIImage imageNamed:model.icon];
    self.title.text = model.title;
    self.buyCount.text = [NSString stringWithFormat:@"需积分:\n%@  分", model.buyCount];
}
// 布局子视图
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat margin = 10;
    CGFloat iconX = margin;
    CGFloat iconY = margin;
    CGFloat iconW = self.bounds.size.width * 0.5 - margin;
    CGFloat iconH = self.bounds.size.height -  2 *margin;
    self.icon.frame = CGRectMake(iconX, iconY, iconW, iconH);
    
    CGFloat titleX = CGRectGetMaxX(self.icon.frame) + margin ;
    CGFloat titleY = iconY + margin;
    CGFloat titleW = iconW;
    CGFloat titleH = 30;
    self.title.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    CGFloat buyCountX = titleX;
    CGFloat buyCountY = CGRectGetMaxY(self.title.frame) + margin;
    CGFloat buyCountW = titleW;
    CGFloat buyCountH = titleH + 2*margin;
    self.buyCount.frame = CGRectMake(buyCountX, buyCountY, buyCountW, buyCountH);
}
// 点击控件，通知代理，跳转
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    if ([self.delegate respondsToSelector:@selector(tuijianViewWithModel:)]) {
        [self.delegate tuijianViewWithModel:self.model];
    }
}
@end
